@tool
extends GridMap


var library:MeshLibrary = mesh_library

@export var expand:int =10 # 内部单向延伸的大小，色块面还会再往外一格
@export var default_block:String = "black" # 留空或者写错名字则相当于填充空气
@export_flags("清空","内支柱","内隔板","外框","国标色块","面十字") var build_structures:int =0 :
	set(value):
		match value:
			1: # 清空
				clear()
			2: # 内支柱
				for i in range(-expand,expand+1):
					set_cell_item(Vector3i(i,0,0),block(default_block))
					set_cell_item(Vector3i(0,i,0),block(default_block))
					set_cell_item(Vector3i(0,0,i),block(default_block))
			4: # 内隔板
				print("内隔板")
				fill(0,-expand,-expand,0,expand,expand,block(default_block))
				fill(-expand,0,-expand,expand,0,expand,block(default_block))
				fill(-expand,-expand,0,expand,expand,0,block(default_block))
			8: # 外框
				for i in range(-expand-1,expand+2):
					set_cell_item(Vector3i(i,expand+1,expand+1),block(default_block))
					set_cell_item(Vector3i(i,-expand-1,expand+1),block(default_block))
					set_cell_item(Vector3i(i,expand+1,-expand-1),block(default_block))
					set_cell_item(Vector3i(i,-expand-1,-expand-1),block(default_block))
					
					set_cell_item(Vector3i(expand+1,i,expand+1),block(default_block))
					set_cell_item(Vector3i(-expand-1,i,expand+1),block(default_block))
					set_cell_item(Vector3i(expand+1,i,-expand-1),block(default_block))
					set_cell_item(Vector3i(-expand-1,i,-expand-1),block(default_block))
					
					set_cell_item(Vector3i(expand+1,expand+1,i),block(default_block))
					set_cell_item(Vector3i(-expand-1,expand+1,i),block(default_block))
					set_cell_item(Vector3i(expand+1,-expand-1,i),block(default_block))
					set_cell_item(Vector3i(-expand-1,-expand-1,i),block(default_block))

			16: # 色块（国际标准）
				# 前绿后蓝 左红右橙 上黄下白
				# x前 z左 y上
				fill((expand+1),-expand,-expand,(expand+1),expand,expand,block("green_glass"))
				fill(-(expand+1),-expand,-expand,-(expand+1),expand,expand,block("blue_glass"))
				fill(-expand,-expand,(expand+1),expand,expand,(expand+1),block("red_glass"))
				fill(-expand,-expand,-(expand+1),expand,expand,-(expand+1),block("orange_glass"))
				fill(-expand,(expand+1),-expand,expand,(expand+1),expand,block("yellow_glass"))
				fill(-expand,-(expand+1),-expand,expand,-(expand+1),expand,block("white_glass"))
			32: # 面十字
				for i in range(-expand,expand+1):
					set_cell_item(Vector3i(i,0,expand+1),block(default_block))
					set_cell_item(Vector3i(i,0,-expand-1),block(default_block))
					set_cell_item(Vector3i(i,expand+1,0),block(default_block))
					set_cell_item(Vector3i(i,-expand-1,0),block(default_block))
					
					set_cell_item(Vector3i(0,i,expand+1),block(default_block))
					set_cell_item(Vector3i(0,i,-expand-1),block(default_block))
					set_cell_item(Vector3i(expand+1,i,0),block(default_block))
					set_cell_item(Vector3i(-expand-1,i,0),block(default_block))
					
					set_cell_item(Vector3i(0,expand+1,i),block(default_block))
					set_cell_item(Vector3i(0,-expand-1,i),block(default_block))
					set_cell_item(Vector3i(expand+1,0,i),block(default_block))
					set_cell_item(Vector3i(-expand-1,0,i),block(default_block))

func block(name:String) -> int: 
	# 想起来在字典里本质上也是要按名字查找，所以二者理论差别不是很大……
	# 估计这个find item by name 也是用字典的
	return library.find_item_by_name(name)
	

func fill(x1:int,y1:int,z1:int,x2:int,y2:int,z2:int,block_id:int):
	# 注意range int 不包含末尾，所以这程序里加了1，这样fill就是mc的那种感觉（包含末尾）
	# godot这里还有个好处，就是fill指令可以接受变量坐标值，而mc的只能输入常量
	#（mc坐标变量目前是用相对坐标与实体配合模拟的）
	for x in range(min(x1,x2),max(x1,x2)+1):
		for y in range(min(y1,y2),max(y1,y2)+1):
			for z in range(min(z1,z2),max(z1,z2)+1):
				set_cell_item(Vector3i(x,y,z),block_id)
